/*******************************************************************************
 * Copyright 2013 Mindaugas Idzelis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package incontrol.bf3.protocol.events.datatype;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class PlayerInfo {

	public final String name;
	public final String guid;
	public final int teamId;
	public final int squadId;
	public final int kills;
	public final int deaths;
	public final int score;

	public PlayerInfo(String name, String guid, int teamId, int squadId,
			int kills, int deaths, int score) {
		super();
		this.name = name;
		this.guid = guid;
		this.teamId = teamId;
		this.squadId = squadId;
		this.kills = kills;
		this.deaths = deaths;
		this.score = score;
	}

	public static List<PlayerInfo> parse(String[] words) {
		int wordCur = 0;
		int params = Integer.parseInt(words[wordCur++]);
		List<String> paramNames = Lists.newArrayList();
		for (int i = 0; i < params; i++) {
			paramNames.add(words[wordCur++]);
		}
		int players = Integer.parseInt(words[wordCur++]);
		Collection<PlayerInfo> playerInfo = Lists.newArrayList();

		for (int j = 0; j < players; j++) {
			Map<String, Object> map = Maps.newHashMap();
			for (int i = 0; i < params; i++) {
				String paramName = paramNames.get(i);
				if ("name".equals(paramName)) {
					String paramValue = words[wordCur++];
					map.put(paramName, paramValue);
				} else if ("guid".equals(paramName)) {
					String paramValue = words[wordCur++];
					map.put(paramName, paramValue);
				} else if ("teamId".equals(paramName)) {
					int paramValue = Integer.parseInt(words[wordCur++]);
					map.put(paramName, paramValue);
				} else if ("squadId".equals(paramName)) {
					int paramValue = Integer.parseInt(words[wordCur++]);
					map.put(paramName, paramValue);
				} else if ("kills".equals(paramName)) {
					int paramValue = Integer.parseInt(words[wordCur++]);
					map.put(paramName, paramValue);
				} else if ("deaths".equals(paramName)) {
					int paramValue = Integer.parseInt(words[wordCur++]);
					map.put(paramName, paramValue);
				} else if ("score".equals(paramName)) {
					int paramValue = Integer.parseInt(words[wordCur++]);
					map.put(paramName, paramValue);
				}
			}
			playerInfo.add(new PlayerInfo((String) map.get("name"),
					(String) map.get("guid"), (Integer) map.get("teamId"),
					(Integer) map.get("squadId"), (Integer) map.get("kills"),
					(Integer) map.get("deaths"), (Integer) map.get("score")));
		}
		return ImmutableList.copyOf(playerInfo);
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(getClass()).add("name", name)
				.add("guid", guid).add("teamId", teamId)
				.add("squadId", squadId).add("kills", kills)
				.add("deaths", deaths).add("score", score).toString();
	}

	public String getName() {
		return name;
	}

	public String getGuid() {
		return guid;
	}

	public int getTeamId() {
		return teamId;
	}

	public int getSquadId() {
		return squadId;
	}

	public int getKills() {
		return kills;
	}

	public int getDeaths() {
		return deaths;
	}

	public int getScore() {
		return score;
	}

}
